home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / filepath.pro < prev    next >
Text File  |  1997-07-08  |  6KB  |  190 lines

  1. ; $Id: filepath.pro,v 1.16 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1989-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6.  
  7. FUNCTION FILEPATH, FILENAME, ROOT_DIR=root_dir, SUBDIRECTORY=subdir, $
  8.     TERMINAL = TERMINAL, TMP = TMP
  9. ;+
  10. ; NAME:
  11. ;    FILEPATH
  12. ;
  13. ; PURPOSE:
  14. ;    Given the name of a file in the IDL distribution,
  15. ;    FILEPATH returns the fully-qualified path to use in
  16. ;    opening the file. Operating system dependencies
  17. ;    are taken into consideration. This routine is used by RSI to
  18. ;    make the User Library portable.
  19. ;
  20. ; CATEGORY:
  21. ;    File Management.
  22. ;
  23. ; CALLING SEQUENCE:
  24. ;    Result = FILEPATH('filename' [, SUBDIRECTORY = subdir])
  25. ;
  26. ; INPUTS:
  27. ;    filename:    The lowercase name of the file to be opened. No device
  28. ;        or directory information should be included.
  29. ;
  30. ; KEYWORDS:
  31. ;    ROOT_DIR: The name of the directory from which the resulting path
  32. ;    should be based. If not present, the value of !DIR is used.
  33. ;    This keyword is ignored if TERMINAL or TMP are specified.
  34. ;
  35. ;    SUBDIRECTORY:    The name of the subdirectory in which the file
  36. ;        should be found. If this keyword is omitted, the main
  37. ;        directory is used.  This variable can be either a scalar
  38. ;        string or a string array with the name of each level of
  39. ;        subdirectory depth represented as an element of the array.
  40. ;
  41. ;        For example, to get a path to the file DETERM in the "userlib"
  42. ;        subdirectory to the IDL "lib" subdirectory, enter:
  43. ;
  44. ;        path = FILEPATH("determ", SUBDIRECTORY = ["lib", "userlib"])
  45. ;
  46. ;    TERMINAL:    Return the filename of the user's terminal.
  47. ;
  48. ;      TMP:    The file is a scratch file.  Return a path to the
  49. ;        proper place for temporary files under the current operating
  50. ;        system.
  51. ;
  52. ; OUTPUTS:
  53. ;    The fully-qualified file path is returned.  If one of the subdirectory
  54. ;    keywords is not specified, the file is assumed to exist in the
  55. ;    main distribution directory.
  56. ;
  57. ; COMMON BLOCKS:
  58. ;    None.
  59. ;
  60. ; RESTRICTIONS:
  61. ;    Don't specify more than one of the keywords in a single call.
  62. ;
  63. ;
  64. ; EXAMPLE:
  65. ;    To get a path to the file DETERM in the "userlib" subdirectory to the
  66. ;    IDL "lib" subdirectory, enter:
  67. ;
  68. ;        path = FILEPATH("determ", SUBDIRECTORY = ["lib", "userlib"])
  69. ;
  70. ;    The variable "path" contains a string that is the fully-qualified file
  71. ;    path for the file DETERM.
  72. ;
  73. ; MODIFICATION HISTORY:
  74. ;    December, 1989, AB, RSI (Formalized from original by DMS)
  75. ;    October, 1990, SG, RSI (added support for MSDOS)
  76. ;    February, 1991, SMR, RSI (added string array support for multi-level
  77. ;                  directories)
  78. ;    21 April 1993, AB, Added ROOT_DIR keyword.
  79. ;       14 July  1994, KDB, RSI - Corrected logic error in VMS section
  80. ;                                 of the ROOT_DIR keyword. Any
  81. ;                                 sub-directory specification was
  82. ;                                 being ignored when using ROOT_DIR. Fixed.
  83. ;    March, 1995, DJE, Add a ':' if root_dir is specified on the Mac.
  84. ;    29 July 1995, Robert.M.Candey.1@gsfc.nasa.gov, Changed VMS case for
  85. ;                no specified path to not append '.][000000]'
  86. ;    April, 1996, DJE, Remove call to STRLOWCASE(SUBDIR).
  87. ;    August, 1996, AJH, used environment variables to define TMP on Win32
  88. ;-
  89.  
  90.  
  91. ON_ERROR,2                                ;Return to caller if an error
  92.                         ;occurs
  93.  
  94. do_tmp = KEYWORD_SET(TMP)            ;get temporary path if existing
  95. path = ''
  96.  
  97. IF (KEYWORD_SET(TERMINAL)) THEN BEGIN
  98.   CASE !VERSION.OS OF
  99.     'vms': RETURN,'SYS$OUTPUT:'
  100.     'Win32': BEGIN
  101.         PRINT, "No terminal device available for Windows"
  102.         RETURN, ''
  103.       END
  104.     'MacOS' : BEGIN
  105.         PRINT, "No terminal device for the Mac"
  106.         RETURN, ''
  107.       END
  108.     ELSE: RETURN,'/dev/tty'
  109.   ENDCASE
  110. ENDIF
  111.  
  112. IF (do_tmp) THEN BEGIN
  113.   CASE !VERSION.OS OF
  114.     'vms': root_dir = 'SYS$LOGIN:'
  115.     'Win32': BEGIN
  116.         root_dir = getenv('TMP')
  117.         IF (strtrim(root_dir, 2) EQ "") THEN BEGIN
  118.             root_dir = GETENV('TEMP')
  119.             IF (strtrim(root_dir, 2) EQ "") THEN BEGIN
  120.                 root_dir = GETENV('WINDIR')
  121.                 IF (strtrim(root_dir, 2) EQ "") THEN root_dir = '\tmp'
  122.             ENDIF
  123.         ENDIF
  124.       END
  125.     'MacOS': begin
  126.     root_dir = !DIR
  127.     if (n_params() EQ 0) then filename = "IDL Temp File"
  128.       end
  129.     ELSE: root_dir = '/tmp'
  130.   ENDCASE
  131. ENDIF ELSE BEGIN
  132.   IF (not KEYWORD_SET(ROOT_DIR)) THEN root_dir = !DIR
  133.   IF (KEYWORD_SET(SUBDIR)) THEN BEGIN        ;if the SUBDIR keyword is set
  134.                         ;then include each level in the
  135.     CASE !VERSION.OS OF                ;path separated by the correct
  136.       'vms': divider = "."            ;directory level delimiter for
  137.       'Win32': divider = "\"        ;the current O.S.
  138.       'MacOS' : divider = ":"
  139.       ELSE: divider = "/"
  140.     ENDCASE
  141.     FOR i = 0, N_ELEMENTS(SUBDIR) - 1 DO BEGIN
  142.     path = path + SUBDIR[i]
  143.     IF(i NE N_ELEMENTS(SUBDIR) - 1) THEN $
  144.       path = path + divider
  145.     ENDFOR
  146.     if !VERSION.OS EQ 'MacOS' THEN path = path + divider
  147.   ENDIF
  148. ENDELSE
  149.  
  150.  
  151. CASE !VERSION.OS OF
  152.   'vms': BEGIN
  153.       IF (NOT do_tmp) THEN BEGIN
  154.     IF (path EQ '') THEN begin
  155.       case 1 of
  156.         strmid(root_dir, strlen(root_dir)-2, 2) eq ".]": $
  157.            root_dir = strmid(root_dir,0,strlen(root_dir)-2) +']' ; remove .
  158.         strmid(root_dir, strlen(root_dir)-1, 1) eq "]": ; nothing
  159.         else: path = '[000000]' ; assume implicit '.]' or device in root_dir
  160.       endcase
  161.     ENDIF ELSE BEGIN ; path is filled
  162.       path = '[' + path + ']'
  163.  
  164.           ; check for a ".]" at the end of our root directory
  165.  
  166.           IF(( strmid(root_dir, strlen(root_dir)-2, 2) ne ".]") and    $
  167.              ( strmid(root_dir, strlen(root_dir)-1, 1) eq "]") )then   $
  168.              root_dir = strmid(root_dir,0,strlen(root_dir)-1) +'.]'
  169.       ; if no ']' at end of root_dir, assume implicit '.]' or device
  170.     ENDELSE
  171.       ENDIF
  172.     END
  173.   'Win32': BEGIN
  174.       path = '\' + path
  175.       IF (path NE '\') THEN path = path + '\'
  176.     END
  177.   'MacOS': BEGIN
  178.       ; make sure the root dir ends with a separator
  179.       IF (STRMID(root_dir, STRLEN(root_dir) - 1, 1) NE ':') THEN $
  180.     root_dir = root_dir + ':'
  181.     END
  182.   ELSE: BEGIN
  183.       path = '/' + path
  184.       IF (path NE '/') THEN path = path + '/'
  185.     END
  186. ENDCASE
  187. RETURN, root_dir + path + filename
  188.  
  189. END
  190.